package com.hoangqp.view;

import java.util.ArrayList;
import java.util.Locale;

import com.hoangqp.lookdict.R;
import com.hoangqp.util.DictionaryItem;
import com.hoangqp.util.ReceiveSaveDictionay;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<Item> implements
		TextToSpeech.OnInitListener {

	// private static final String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	TextToSpeech talker;
	Context context;
	int layoutResource;
	// ArrayList<DictionaryItem> data = new ArrayList<DictionaryItem>();
	ArrayList<Item> data = new ArrayList<Item>();

	private LayoutInflater vi;

	public CustomListViewAdapter(Context context, int resource,
			ArrayList<Item> data) {
		super(context, resource, data);
		// TODO Auto-generated constructor stub
		this.talker = new TextToSpeech(context, this);
		this.context = context;
		this.layoutResource = resource;
		this.data = data;
		vi = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = convertView;
		RecordHolder holder = null;

		final Item i = data.get(position);

		if (row == null) {
			if (i.isSection()) {
				SectionItem si = (SectionItem) i;
				row = vi.inflate(R.layout.top_list_item_section, null);

				row.setOnClickListener(null);
				row.setOnLongClickListener(null);
				row.setLongClickable(false);
				row.setClickable(false);

				final TextView sectionView = (TextView) row
						.findViewById(R.id.list_item_section_text);
				sectionView.setText(si.getTitle());
			} else {
				EntryItem ei = (EntryItem) i;
				row = vi.inflate(R.layout.custom_row, null);

//				LayoutInflater inflater = ((Activity) context)
//						.getLayoutInflater();
//				row = inflater.inflate(layoutResource, parent, false);

				holder = new RecordHolder();
				holder.txtDictionary = (TextView) row
						.findViewById(R.id.txt_item_dictionary);

				holder.btnSpeech = (Button) row
						.findViewById(R.id.speech_item);

				holder.btnShare = (Button) row.findViewById(R.id.btn_share_dictionary);
				
				holder.btnSave = (Button) row.findViewById(R.id.btn_save_dictionary);
				
				holder.btnShare.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.d("CLICKED", "SHARE BUTTON!");
					}
				});
				
				holder.btnSave.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.d("CLICKED", "SAVE BUTTON!");
						Intent i = new Intent(context, ReceiveSaveDictionay.class);                 
						i.putExtra("new value", "50");                 
						context.sendBroadcast(i);
					}
				});
				final Item item = data.get(position);
				if(!item.isSection()){
					EntryItem entry = (EntryItem) item;
					final DictionaryItem itemDict = entry.getWordEntry();
					
					holder.txtDictionary.setText(itemDict.getDictionary());
					//holder.btnSpeech.setBackgroundColor(Color.alpha(0));
					holder.btnSpeech.setClickable(true);
					holder.btnSpeech.setOnClickListener(new OnClickListener() {
			
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							speaker(itemDict);
						}
					});
				}
				
				row.setTag(holder);
			}

		} else {
			holder = (RecordHolder) row.getTag();
		}

		return row;
	}

	@Override
	public int getViewTypeCount() {
		return getCount();
	}

	@Override
	public int getItemViewType(int position) {
		return position;
	}

	protected void speaker(final DictionaryItem item) {
		// TODO Auto-generated method stub
		String text = item.getDictionary();

		talker.speak(text, TextToSpeech.QUEUE_FLUSH, null);
	}

	static class RecordHolder {

		// front
		TextView txtDictionary;
		Button btnSpeech;

		// back
		Button btnShare;
		Button btnSave;
	}

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
		if (status == TextToSpeech.SUCCESS) {

			int result = talker.setLanguage(Locale.US);

			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Log.e("TTS", "This Language is not supported");
			} else {
				// btnSpeak.setEnabled(true);
				// speaker();
			}

		} else {
			Log.e("TTS", "Initilization Failed!");
		}
	}

	public TextToSpeech getSpeaker() {
		return talker;
	}
}
